package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.AdvisoryDto;
import com.csun.cmny.provider.dto.AdvisoryQueryDto;
import com.csun.cmny.provider.model.domain.OmcAdvisory;
import com.csun.cmny.provider.model.domain.OmcLevelCare;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcAdvisoryService;
import com.csun.cmny.provider.service.OmcLevelCareService;
import com.csun.cmny.provider.validator.OmcAdvisoryAddValidator;
import com.csun.cmny.provider.validator.OmcAdvisoryEditValidator;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/advisory", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcAdvisoryController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcAdvisoryService omcAdvisoryService;

    @Resource
    private OmcLevelCareService levelCareService;

    /**
     * 咨询记录-添加
     * @param: [advisoryDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addAdvisory(@Validated(OmcAdvisoryAddValidator.class) @RequestBody AdvisoryDto advisoryDto,
                           BindingResult br){
        advisoryDto.setGroupId(getLoginAuthDto().getGroupId());

        Integer result = saveAdvisory(advisoryDto);

        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 咨询记录-修改
     * @param: [advisoryDto, br]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/edit")
    public Wrapper editAdvisory(@Validated(OmcAdvisoryEditValidator.class) @RequestBody AdvisoryDto advisoryDto,
                               BindingResult br){
        advisoryDto.setGroupId(getLoginAuthDto().getGroupId());
        Integer result = saveAdvisory(advisoryDto);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 根据床位id查询咨询记录
     * @param: [bedId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/getAdvisoryByBedId", method = RequestMethod.POST)
    public Wrapper getAdvisoryByBedId(@PathVariable Integer bedId) {

        Preconditions.checkArgument(bedId != null, cmnyMessageSource
                .getMessage("bed.id.null", null));
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), null);
    }

    /**
     * 咨询记录-删除
     * @param: [advisoryId]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/deleteById/{advisoryId}", method = RequestMethod.POST)
    public Wrapper deleteAdvisory(@PathVariable Long advisoryId) {

        // 校验advisoryId是否为空
        Preconditions.checkArgument(advisoryId != null, cmnyMessageSource
                .getMessage("advisory.id.null", null));

        Integer result = omcAdvisoryService.deleteAdvisoryById(advisoryId,getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 咨询记录查询
     * @param: [advisoryQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryAdvisoryListWithPage(@RequestBody AdvisoryQueryDto advisoryQueryDto) {

        advisoryQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        PageInfo pageInfo = omcAdvisoryService.queryAdvisoryListWithPage(advisoryQueryDto);
//        return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

    /**
     * 保存咨询记录
     * @param: [advisoryDto]
     * @return: java.lang.Integer
    */
    public Integer saveAdvisory(AdvisoryDto advisoryDto) {
        OmcAdvisory omcAdvisory = new OmcAdvisory();
        BeanUtils.copyProperties(advisoryDto, omcAdvisory);

        if (PublicUtil.isNotEmpty(advisoryDto.getLevelCareId())){
            OmcLevelCare omcLevelCare = levelCareService.selectByKey(advisoryDto.getLevelCareId());
            if (PublicUtil.isNotEmpty(omcLevelCare)){
                omcAdvisory.setLevelCareId(omcLevelCare.getId());
                omcAdvisory.setNursingCare(omcLevelCare.getNursingCare());
            }
        }
        return omcAdvisoryService.saveAdvisory(omcAdvisory, getLoginAuthDto());
    }

}
